package com.ld.admin.controller.system;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.shieldsb.common.core.model.PropertiesModel;
import com.ld.shieldsb.common.core.util.StringUtils;
import com.ld.shieldsb.common.core.util.properties.PropertiesPojo;
import com.ld.shieldsb.common.web.util.Web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 配置文件管理
 * 
 * @ClassName PropertiesController
 * @author 吕凯
 * @date 2019年02月19日 11:02:27
 *
 */
@Api(tags = "配置文件管理")
@ConditionalOnProperty(prefix = "modules.sc.system", name = "properties.enable", havingValue = "true", matchIfMissing = true)
@Controller(value = "systemPropertiesController")
@RequestMapping("${shieldsb.adminPath}/properties")
public class PropertiesController extends AdminBasicController<Object> {

    @ApiOperation(value = "到配置页面", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String config() {
        String type = getString("ty");
        setAttributes();
        List<PropertiesPojo> configList = null;
        if ("project".equals(type)) {
            configList = PropertiesModel.PROJECT.getPropertys();
        } else {
            configList = PropertiesModel.CONFIG.getPropertys();
        }
        request.setAttribute("configs", configList);
        return "a/system/properties/properties_list";
    }

    @ApiOperation(value = "跳转到更新页面", notes = "")
    @RequestMapping(path = "/toUpdate", method = RequestMethod.GET)
    public String config2oUpdate() {
        setAttributes();
        String key = getString("key");
        if (StringUtils.isNotBlank(key)) {
            String type = getString("ty");
            PropertiesPojo model = null;
            if ("project".equals(type)) {
                model = PropertiesModel.PROJECT.getProperty(key);
            } else if ("config".equals(type)) {
                model = PropertiesModel.CONFIG.getProperty(key);
            }
            request.setAttribute("model", model);
        }
        return "a/system/properties/properties_update";
    }

    @ApiOperation(value = "更新配置信息", notes = "")
    @RequestMapping(path = "/update", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object configUpdate(String... args) throws ServletException, IOException {
        String key = getString("key");
        String value = getString("value");
        String comment = getString("comment");
        String type = getString("ty");
        if ("project".equals(type)) {
            PropertiesModel.PROJECT.save(key, value, comment);
        } else if ("config".equals(type)) {
            PropertiesModel.CONFIG.save(key, value, comment);
        }
        return Web.Response.success("更新配置成功！");
    }

}